package top.jiangqiang.qyblog.core.service.impl;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import org.springframework.stereotype.Component;
import top.jiangqiang.qyblog.common.config.GlobalProperties;

/**
 * @author JiangQiang
 */
@Component
public class SecureService {
    private final AES aes;

    public SecureService(GlobalProperties globalProperties) {
        this.aes = SecureUtil.aes(HexUtil.decodeHex(globalProperties.getSecurity().getPassword()));
    }

    /**
     * 加密
     *
     * @param data 数据
     * @return 加密后的Hex
     */
    public String encryptHex(String data) {
        return aes.encryptHex(data);
    }

    /**
     * 解密Hex（16进制）或Base64表示的字符串
     *
     * @param data 被解密的String
     * @return 解密后的String
     */
    public String decryptStr(String data) {
        return aes.decryptStr(data);
    }
}
